package com.pbs.net;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.pbs.contact.Contact;
import com.pbs.packdata.BackupInfo;
import com.pbs.packdata.SenderContactInfo;
import com.pbs.packdata.UserInfo;
import com.pbs.utils.MyXStream;

public class WSF {

	public enum Result {
		SUCCESSED, FAILED, EXCEPTION;
	}

	// OK
	public static boolean backupContactList(ArrayList<Contact> list,
			int userid, String note, String date, String time) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/BackupContact";
		String METHOD_NAME = "BackupContact";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=BackupContact";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		String content = MyXStream.toXML(list);
		Log.i("XStream Bitmap", "Done");
		request.addProperty("XML", content);
		request.addProperty("USERID", userid);
		request.addProperty("NOTE", note);
		request.addProperty("DATE", date);
		request.addProperty("TIME", time);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult.equalsIgnoreCase("true");
	}

	public static Result addUser(int from, String username, String display_name)
			throws Exception {
		String SOAP_ACTION = "http://tempuri.org/AddUsername";
		String METHOD_NAME = "AddUsername";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=AddUsername";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("from", from);
		request.addProperty("username", username);
		request.addProperty("display_name", display_name);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		if (result.equals("-1")) {
			return Result.EXCEPTION;
		} else if (result.equals("0")) {
			return Result.FAILED;
		} else {
			return Result.SUCCESSED;
		}
	}

	public static int removeUser(int from, String username) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/RemoveUsername";
		String METHOD_NAME = "RemoveUsername";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=RemoveUsername";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("from", from);
		request.addProperty("username", username);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		int count = Integer.parseInt(result.toString().trim());
		return count;
	}

	public static Result updateUser(int from, String display_name, String id) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/EditFriend";
		String METHOD_NAME = "EditFriend";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=EditFriend";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("from", from);
		request.addProperty("display_name", display_name);
		request.addProperty("id", id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		if (result.equals("-1")) {
			return Result.EXCEPTION;
		} else if (result.equals("0")) {
			return Result.FAILED;
		} else {
			return Result.SUCCESSED;
		}
	}

	public static String sendEmail(String to) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/sendEmail";
		String METHOD_NAME = "sendEmail";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=sendEmail";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("from", "tranhuuphuoc88@gmail.com");
		request.addProperty("to", to);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		return result.toString();
	}

	/**
	 * Recover password from email
	 * @param username Get password and email from username
	 * @return HashMap<String, String> with key "email" to get value email, key "password" to get value password
	 * @throws Exception
	 */
	public static HashMap<String, String> getEmailPasswordFromUsername(String username)throws Exception{
		String SOAP_ACTION = "http://tempuri.org/GetEmailPasswordFromUsername";
		String METHOD_NAME = "GetEmailPasswordFromUsername";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=GetEmailPasswordFromUsername";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("username", username);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		HashMap<String, String> email_password = new HashMap<String, String>();
		String[] temp = result.toString().split("/");
		if(temp==null||temp.length!=2){
			return null;
		}
		email_password.put("email", temp[0]);
		email_password.put("password", temp[1]);
		return email_password;
	}
	
	public static String getAccountConnectedToEmail(String email) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/GetAccountFromEmail";
		String METHOD_NAME = "GetAccountFromEmail";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=GetAccountFromEmail";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("email", email);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		return result.toString().trim();
	}

	public static ArrayList<UserInfo> getListUsername(int userid)
			throws Exception {
		String SOAP_ACTION = "http://tempuri.org/GetListUsernameOf";
		String METHOD_NAME = "GetListUsernameOf";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=GetListUsernameOf";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("userid", userid);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);
		
		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
		SoapObject a = (SoapObject) resultsRequestSOAP.getProperty(0);
		int count = a.getPropertyCount();
		Log.i("Count user", ""+count);
		ArrayList<UserInfo> bki = new ArrayList<UserInfo>();
		for (int i = 0; i < count; i++) {
			SoapObject so = (SoapObject) a.getProperty(i);
			String us = so.getProperty("display_name").toString();
			String rname = so.getProperty("username").toString();
			String id = so.getProperty("id").toString();
			bki.add(new UserInfo(id, us, rname));
		}
		return bki;
	}

	public static ArrayList<BackupInfo> getAllBackupInfo(int userid)
			throws Exception {
		String SOAP_ACTION = "http://tempuri.org/GetAllBackupInfo";
		String METHOD_NAME = "GetAllBackupInfo";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=GetAllBackupInfo";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("userid", userid);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
		SoapObject a = (SoapObject) resultsRequestSOAP.getProperty(0);
		int count = a.getPropertyCount();
		Log.i("Get backup Info", "Size: "+count);
		ArrayList<BackupInfo> bki = new ArrayList<BackupInfo>();
		for (int i = 0; i < count; i++) {
			SoapObject so = (SoapObject) a.getProperty(i);
			String date = so.getProperty("date").toString();
			String time = so.getProperty("time").toString();
			String note = so.getProperty("notes").toString().trim();
			String id = so.getProperty("ID").toString();
			bki.add(new BackupInfo(date, time, note, id));
		}
		return bki;
	}

	public static boolean deleteBackupVersion(String id) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/DeleteBackupVersion";
		String METHOD_NAME = "DeleteBackupVersion";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=DeleteBackupVersion";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("id", id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return Boolean.parseBoolean(myResult);
	}

	// Pending webservice
	public static ArrayList<Contact> restoreContactList(String version_id) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/RestoreContact";
		String METHOD_NAME = "RestoreContact";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=RestoreContact";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("version_id", version_id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		Log.i("reverting...", myResult);
		if (myResult.trim().equals("")) {
			return null;
		}
		return MyXStream.fromXML(myResult);
	}

	public static boolean updateContentSender(String xml_content, String id)
			throws Exception {
		String SOAP_ACTION = "http://tempuri.org/UpdateContentSender";
		String METHOD_NAME = "UpdateContentSender";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=UpdateContentSender";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("xml_content", xml_content);
		request.addProperty("id", id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult.equals("true");
	}

	public static String sentContactList(int sender_id,String sender, String receiver,
			ArrayList<Contact> content, String date, String time, String note)
			throws Exception {
		String SOAP_ACTION = "http://tempuri.org/SendContact";
		String METHOD_NAME = "SendContact";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=SendContact";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		
		String xml_content = MyXStream.toXML(content);
		request.addProperty("sender_id", sender_id);
		request.addProperty("sender", sender);
		request.addProperty("receiver", receiver);
		request.addProperty("xml_content", xml_content);
		request.addProperty("date", date);
		request.addProperty("time", time);
		request.addProperty("note", note);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();

		return myResult;
	}

	public static ArrayList<Contact> getPakageContact(String id_package)
			throws Exception {
		String SOAP_ACTION = "http://tempuri.org/ReceivePackageContact";
		String METHOD_NAME = "ReceivePackageContact";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=ReceivePackageContact";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("id_package", id_package);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		if (myResult.trim().equals(""))
			return new ArrayList<Contact>();
		return (ArrayList<Contact>) MyXStream.fromXML(myResult);
	}

	public static ArrayList<SenderContactInfo> getSenderList(String receiver) {
		String SOAP_ACTION = "http://tempuri.org/GetSenderList";
		String METHOD_NAME = "GetSenderList";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=GetSenderList";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("receiver", receiver);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
		} catch (Exception e) {
			return null;
		}
		SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
		SoapObject a = (SoapObject) resultsRequestSOAP.getProperty(0);
		int count = a.getPropertyCount();
		ArrayList<SenderContactInfo> senderList = new ArrayList<SenderContactInfo>();
		for (int i = 0; i < count; i++) {
			SoapObject so = (SoapObject) a.getProperty(i);
			String date = so.getProperty("date").toString();
			String time = so.getProperty("time").toString();
			String sender = so.getProperty("sender").toString();
			String note = so.getProperty("note").toString();
			String id = so.getProperty("id").toString();
			senderList.add(new SenderContactInfo(date, time, sender, note, id));
		}
		return senderList;
	}

	public static boolean deleteSender(String id) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/DeleteSender";
		String METHOD_NAME = "DeleteSender";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=DeleteSender";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("id", id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult.equals("true");
	}

	// OK
	public static String checkLogin(String username, String password)
			throws Exception {
		String SOAP_ACTION = "http://www.naveenbalani.com/webservices/WassupAndroidService/todaysMessage";//"http://tempuri.org/CheckLogin";
		String METHOD_NAME = "todaysMessage";//"CheckLogin";
		String NAMESPACE = "http://www.naveenbalani.com/webservices/WassupAndroidService/";//"http://tempuri.org/";
		String URL = "http://naveenbalani.com/WassupAndroid.asmx";//"http://thphuoc.somee.com/Service1.asmx?op=CheckLogin";//?op=CheckLogin";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//		request.addProperty("username", username);
//		request.addProperty("password", password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				URL);
		Log.i("CheckLogin:", "496 - Calling function...");
		androidHttpTransport.call(SOAP_ACTION, envelope);
		Log.i("CheckLogin:", "496 - passed");		
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		Log.i("CheckLogin:", "498 - get response");
		String myResult = result.toString();
		return myResult;

	}

	// OK
	public static String createAccount(String username, String password,
			String email) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/CreateAccount";
		String METHOD_NAME = "CreateAccount";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=CreateAccount";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("username", username);
		request.addProperty("password", password);
		request.addProperty("email", email);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult;
	}

	public static boolean changePassword(String usernameCurrent,
			String newPassword) throws Exception {
		String SOAP_ACTION = "http://tempuri.org/ChangePassword";
		String METHOD_NAME = "ChangePassword";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=ChangePassword";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("usernameCurrent", usernameCurrent);
		request.addProperty("newPassword", newPassword);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult.equalsIgnoreCase("true");
	}

	public static int getNewContactCount(String username) {
		String SOAP_ACTION = "http://tempuri.org/GetNewContactCount";
		String METHOD_NAME = "GetNewContactCount";
		String NAMESPACE = "http://tempuri.org/";
		String URL = "http://thphuoc.somee.com/Service1.asmx?op=GetNewContactCount";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("username", username);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
			String myResult = result.toString();
			try {
				return Integer.parseInt(myResult.trim());
			} catch (Exception e) {
				return -1;
			}
		} catch (Exception e) {
			return -1;// Exception when trying connect to webservice
		}
	}
}
